<template>
	<view>
		<view class="head">
			<TbarNav title=" " :isbg="1" :stickys="true"></TbarNav>
			<view class="top-bg" :style="'background-image: url('+ infoData.image +');'">
			</view>
			
			<view class="top-con">
				<view class="title">
					<text></text>
					<text>{{infoData.title}}</text>
				</view>
				
				<view class="date">
					<view class="date-title">
						请选择预定日期
					</view>
					<view class="date-list">
						
						<scroll-view class="date-list-con-scroll" 
							scroll-x="true"
							:scroll-left="scrollLeft"
							@scroll="onScroll"
							scroll-with-animation
							>
							<view class="date-list-con">
							<view class="li"
								@click="onDateClick(index)"
								:class="index == dateIndex ? 'on': ''" 
								v-for="(item, index) in dateList"
								:key="index"
								>
								<text>{{item.date}}</text>
								<text>{{item.dayOfWeek}}</text>
							</view>
							</view>
						</scroll-view>
							
						
						<view class="date-list-icon" @click="goRight">
							<image :src="baseUrl +'/static/wximages/icon-14.png'" mode="widthFix"></image>
						</view>
					</view>
					<view class="tips">
						每个时间段，最多可以预约4个人
					</view>
					<view class="date-title">
						请选择时间段
					</view>
					<view class="date-period">
						<view class="li"
							  @click="onTimeSlotsClick(index)"
							  :class="index == timeSlotsIndex ? 'on' : '' "
							  v-for="(item, index) in timeSlots"
							  :key="index"
							  >
							<view class="lr" :class="item.is_full ? '' : 'lan' ">{{item.is_full ?'满':'空'}}</view>
							<view class="man">{{item.start_time}}--{{item.end_time}}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<view class="btns">
			<button @click="onSubmit">立即预约</button>
		</view>
	</view>
</template>

<script>
	import TbarNav from '@/components/TbarNav'
	
	import { getCardInfo,
			getProjectDateInfo,
			cardTimeSlots,
			cardOrder } from '@/api/reserve.js';
	
	export default {
		components: {
			TbarNav
		},
		data() {
			return {
				id: '',
				infoData: {},
				dateList: [],
				dateIndex: 0,
				
				
				timeSlots: [],
				timeSlotsIndex: 0,
				
				scrollLeft: 0,
				baseUrl: this.$baseUrl
			}
		},
		onLoad(ops) {
			if (ops.id) {
				this.id = ops.id;
			}
		},
		onShow() {
			this.getProjectDateInfo();
			this.getCardInfo()
		},
		methods: {
			onJmup(url) {
				uni.navigateTo({
					url: url
				})
			},
			getCardInfo(){
				getCardInfo({
					id: this.id
				}).then(res => {
					this.infoData = res.data;
				})
			},
			goRight() {
				// console.log('this.scrollLeft', this.scrollLeft);
				let randomNum = Math.random();
			
				this.scrollLeft = 120+randomNum;
			},
			onScroll(event) {
				console.log(event.detail);
				// this.scrollLeft = event.detail.scrollLeft;
			},
			onDateClick(index) {
				this.dateIndex = index;
				this.getTimeSlots()
			},
			getProjectDateInfo() {
				getProjectDateInfo().then(res => {
					this.dateList = res.data;
					
					this.getTimeSlots()
				})
			},
			
			
			onTimeSlotsClick(index) {
				if (this.timeSlots[index].is_full) {
					return this.$util.Tips({
						title: '当前时间段已满人'
					});
					return false;
				}
				this.timeSlotsIndex = index;
			},
			
			getTimeSlots() {
				cardTimeSlots({
					date: this.dateList[this.dateIndex].date_nyr,
					id: this.id
				}).then(res => {
					this.timeSlots = res.data;
				})
			},
			
			
			
			onSubmit() {
				var date = this.dateList[this.dateIndex].date_nyr
				var timeslots = this.timeSlots[this.timeSlotsIndex].start_time+'-'+this.timeSlots[this.timeSlotsIndex].end_time
				var formData = {};
				formData['date'] = date;
				formData['timeslots'] = timeslots;
				formData['cid'] = this.id
				uni.showLoading({
					title: '预定中...'
				});
				cardOrder(formData).then(res => {
					this.getTimeSlots();
					
					return this.$util.Tips({
						title: res.msg,
						icon: 'success'
					}, {
						tab: 2,
						url: '/pages/reserve/card/record'
					});
					
				}).catch(err => {
					uni.hideLoading();
					return this.$util.Tips({
						title: err
					});
				})
			}
			
			
		}
	}
</script>
<style>
	page {
		background: #003153;
	}
</style>
<style scoped lang="scss">
	.head {
		position: relative;
		width: 750rpx;
		height: 630rpx;
		background-repeat: no-repeat;
		background-size: cover;
		.top-bg {
			position: absolute;
			left: 0;
			top: 0;
			bottom: 0;
			right: 0;
			border-radius: 10px 10px 0 0;
			background-repeat: no-repeat;
			background-size: cover;
			opacity: .4;
		}
		.top-con {
			position: relative;
			left: 0;
			top: 0;
			bottom: 0;
			right: 0;
			z-index: 1;
			.title {
				padding: 22rpx;
				font-size: 35rpx;
				color: #3D3D3D;
				display: flex;
				flex-direction: column;
				align-items: center;
				text {
					&:nth-child(2) {
						font-weight: 500;
						font-size: 40rpx;
						color: #FFFFFF;
						margin-top: 14rpx;
					}
				}
			}
			
			.date {
				width: 706rpx;
				// height: 511rpx;
				background: #FFFFFF;
				margin: 50rpx auto;
				padding: 30rpx;
				border-radius: 20rpx 20rpx 20rpx 20rpx;
				.date-title {
					font-weight: 500;
					font-size: 30rpx;
					color: #3D3D3D;
				}
				.tips {
					font-size: 35rpx;
					color: #3D3D3D;
					text-align: center;
					padding: 25rpx 0;
				}
				.date-list {
					display: flex;
					align-items: center;
					justify-content: space-between;
					margin: 38rpx 0;
					.date-list-con-scroll {
						white-space: nowrap;
						width: 100%;
						width: 600rpx;
					}
					.date-list-con {
						display: flex;
						align-items: center;
						.li {
							flex: 0 0 auto;
							width: 95rpx;
							height: 95rpx;
							border-radius: 20rpx 20rpx 20rpx 20rpx;
							margin-right: 26rpx;
							font-size: 23rpx;
							color: #000000;
							display: flex;
							align-items: center;
							justify-content: center;
							flex-direction: column;
							&.on {
								width: 95rpx;
								height: 95rpx;
								background: #E4F1FF;
								border-radius: 20rpx 20rpx 20rpx 20rpx;
								color: #1677FF;
							}
						}
					}
					.date-list-icon {
						image {
							flex: 0 0 13rpx;
							width: 13rpx;
							height: 14rpx;
						}
						
					}
				}
				
				.date-period {
					display: flex;
					align-items: center;
					flex-wrap: wrap;
					overflow: auto;
					margin-top: 33rpx;
					.li {
						width: 202rpx;
						height: 69rpx;
						background: #F9FBFC;
						border-radius: 5rpx 5rpx 5rpx 5rpx;
						border: 1rpx solid #F9FBFC;
						margin-right: 20rpx;
						margin-bottom: 20rpx;
						display: flex;
						align-items: center;
						&:nth-child(3n+3) {
							margin-right: 0;
						}
						&.on {
							background: #E4F1FF;
							// .lr {
							// 	background: #78E383;
							// }
							// .man {
							// 	background: #E4F1FF;
							// }
						}
						.lr {
							flex: 0 0 55rpx;
							width: 55rpx;
							height: 69rpx;
							background: #E6E6E6;
							font-size: 20rpx;
							color: #F9FBFC;
							text-align: center;
							line-height: 69rpx;
							&.lan {
								background: #78E383;
							}
						}
						.man {
							width: 100%;
							font-size: 20rpx;
							color: #3D3D3D;
							text-align: center;
							line-height: 69rpx;
						}
					}
				}
				
				
				
			}
		}
	}
	.btns {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 9;
		padding: 20rpx 47rpx;
		button {
			width: 651rpx;
			height: 95rpx;
			background: #FFFFFF;
			border-radius: 10rpx 10rpx 10rpx 10rpx;
			font-size: 35rpx;
			color: #000000;
			text-align: center;
			line-height: 95rpx;
		}
	}
</style>